package com.baitengsoft.patentapimonitor.api.controller;

import com.baitengsoft.patentapimonitor.api.entity.WebPlatformEntity;
import com.baitengsoft.patentapimonitor.api.entity.WebUserinfoEntity;
import com.baitengsoft.patentapimonitor.api.model.param.EditPlatformParam;
import com.baitengsoft.patentapimonitor.api.service.WebPlatformService;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 时间范围管理
 * @author Chen 2017/12/20
 */
@RestController
@ResponseBody
@RequestMapping("/api/v1/web")
@Api(value = "前台-时间范围管理",description = "时间范围管理")
public class WebTimeRangesController extends AbstractController {
    @Autowired
    private WebPlatformService webPlatformService;

    @ApiOperation(value = "修改时间范围")
    @PostMapping("/sys/timeranges")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "yearFeeExpiredMonths", value = "即将过年费缴纳期月(1,2,3)", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "expiredMonths", value = "即将届满月(1,3,6,12,24)", required = true)
    })
    public Res changeTimeRanges(@ApiIgnore EditPlatformParam param){
        if (param ==null || param.getYearFeeExpiredMonths() ==null || param.getExpiredMonths() ==null){
            return Res.error("请求参数有误");
        }
        WebUserinfoEntity userInfo = getUserInfo();
        if(userInfo == null || userInfo.getPlatformid()==null){
            return Res.error("用户登录信息失效");
        }
        param.setPlatformid(userInfo.getPlatformid());
       return webPlatformService.updateTimeRanges(param);
    }

    @ApiOperation(value = "获取现有时间范围")
    @GetMapping("/sys/timeranges")
    public Res getTimeRanges(){
        WebUserinfoEntity userInfo = getUserInfo();
        if(userInfo == null || userInfo.getPlatformid()==null){
            return Res.error("用户登录信息失效");
        }
        WebPlatformEntity webPlatformEntity = webPlatformService.queryById(userInfo.getPlatformid());
        if (webPlatformEntity == null){
            return Res.error("获取平台信息失败");
        }
        Integer yearfeeexpiredmonths = webPlatformEntity.getYearfeeexpiredmonths();
        if (yearfeeexpiredmonths == null)  yearfeeexpiredmonths =1;
        Integer expiredmonths = webPlatformEntity.getExpiredmonths();
        if (expiredmonths == null) expiredmonths =1;
        return Res.ok()
                .put("yearFeeExpiredMonths", yearfeeexpiredmonths)
                .put("expiredMonths", expiredmonths);
    }


}
